1

My MySQL database is setup with the name "chatterr" with the table name, "maillist". I am currently using the php below:

<?php
mysql_connect("localhost","root","");

//query databae
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);

while($row<$numrows) {
    $id=mysql_result($result,$row,"id");
    $first_name=mysql_result($result, $row, "first_name");
    $last_name=mysql_result($result, $row, "last_name");
?>

<?php echo $id; ?>

<?php
$row++;
}
?>

It works on localhost but doesn't work in PHP. What's wrong with the code?

16
  • Why do you have several different <?php ... ?> tags? Wrap all of your php code in just one set of <?php ... ?> tags. You are splitting up your while loop. Commented Mar 10, 2013 at 6:05
  • @Aiias Thats irrelevant to the question, but I am doing that because there is html in between that i didn't upload, since its irrelevant. Commented Mar 10, 2013 at 6:07
  • 1
    Please, don't use mysql_* functions, they are officially deprecated. Commented Mar 10, 2013 at 6:08
  • @Harrison Howard - It's not irrelevant because you are splitting up your while loop and it will not work as you expect it to. Try using the alternate syntax for php while loops if you need to split up your code into several php code snippets. Commented Mar 10, 2013 at 6:09
  • What does "doesn't work" mean? "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get incorrect results? Did you get no results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get any correct results? If so, what were they? Don't make us guess. Commented Mar 10, 2013 at 6:09

2 Answers 2

3

Select a database with mysql_select_db before querying it

mysql_connect("localhost","root","");
mysql_select_db("chatterr");

or specify the database name in the query

$query = "SELECT * FROM chatterr.students ORDER BY id DESC LIMIT 4";

UPDATE: Besides that your is connection probably failing. Change

mysql_connect("localhost","root","");

to

$db = mysql_connect("localhost","root","");
if (!$db) {
    die('Could not connect: ' . mysql_error());
}

to see if that's the case.

UPDATE3: Put it all together. Although that code has a LOT room for improvement it works perfectly fine on my machine.

<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("chatterr", $db);
if (!$db_selected) {
    die ('Could not select db: ' . mysql_error());
}

//query databae
$query = "SELECT * FROM test.students ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);

while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result,$row,"first_name");
$last_name=mysql_result($result,$row,"last_name"); ?>

<?php echo $id; ?>

<?php

$row++;
}
?>

And please, don't use mysql_* functions for new code. They are deprecated. Use prepared statements with either PDO or MySQLi.

Sign up to request clarification or add additional context in comments.

9 Comments

So would the dbname be "chatterr"?
If it is your database name then yes.
Wait the database name or table name?
Its not my connection, still getting: Parse error: syntax error, unexpected T_STRING in /Applications/XAMPP/xamppfiles/htdocs/chatterr/new/core/dbconnect.php on line 10. Line 10 is $result=mysql_query($query) or die mysql_error();
That's because you forgot parentheses in die(). It should be $result=mysql_query($query) or die(mysql_error());
|
0

Just do it this way :

<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chatterr",$con) or die(mysql_error());

//query database
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
    $id = $row["id"];
    $first_name = $row["first_name"];
    $last_name = $row["last_name"];
}
?>

6 Comments

I am still getting "Error, insert query failed".
I just updated my code, you are having error in mysql query. Instead of printing Error Message, it's better to print mysql query error. Try that
all it says is "No database selected".
I just updated my code, Now try it, it will 100% work. You didn't select the database in connectivity.
Still staying no database. WHY? Idk why. I have code to select it.
|

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.