user978014
user978014

Reputation: 1

Database in PHP

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);

Upvotes: 0

Views: 104

Answers (2)

Atheist
Atheist

Reputation: 525

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.

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14862

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>";
}

Upvotes: 1

Related Questions