0

I'm new to PHP, and I keep getting errors with this line:

$query2 = "insert into student(regno, name) values('100', 'abc')";

I think I've got the syntax correct, but I'm not sure.

The error given is:

Parse error: syntax error, unexpected T_LNUMBER in C:\wamp\www\mydb2.php on line 7

The rest of my code is:

$con = mysql_connect("localhost", "root", "");

mysql_select_db("mydb", $con);

$query1 = "create table student(
    regno int primary key, 
    name varchar(10) NOT NULL)";

$query2 = "insert into student(regno, name) values('100', 'abc')";

if(result1 = mysql_query($query1, $con))
{
    echo "table created";
}

if(result2 = mysql_query($query2, $con))
{
    echo "insert successfull";
}

while(mysql_fetch_rows(result2))
{
    echo "<table><th>regno</th><th>name</th>";
    echo "<tr><td>regno[0]</td><td>name[0]</td></tr></table>";
}

mysql_close($con);
1
  • When writing SQL statements, a good practice is to capitalise them, i.e. "INSERT INTO table_name (col1, col2) VALUES ('val1', 'val2')" that way it is easier to see what is MySQL and what is your other code. Commented Oct 4, 2011 at 8:21

2 Answers 2

1

Your variables for the queries do not start with the $sign - result1 => $result1, and result2 => $result2.

[edit]

Also - Your while loop is not going to work.Query2 is performing an insert, it will not return a value (actually, it will return boolean true or false). If you are trying to get values out of the db as well, then you need to perform a select query:

<?php
//...
$query3 = "select * from student";
$result3 = mysql_query($query3);

while($row = mysql_fetch_array($result3))
{
    echo"<table><th>regno</th><th>name</th>";
    echo"<tr><td>{$row['regno']}</td><td>{$row['name']}</td></tr></table>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

you've forgotten $ in front of result1 and result2:

$query2="insert into student(regno,name) values('100','abc')";
       if($result1=mysql_query($query1,$con))
       echo"table created";
       if($result2=mysql_query($query2,$con))

[Edit]

Generally check that you have remembered $in front of your variable-names, there are several place you've forgotten them.

2 Comments

Also, $regno[0] and $name[0]
And the array references won't work in the double quoted string. They should be concatenated instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.