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?

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
Harrison Howard
  • 153
  • 3
  • 4
  • 14
  • Why do you have several different `` tags? Wrap all of your php code in just one set of `` tags. You are splitting up your while loop. – Aiias Mar 10 '13 at 06: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. – Harrison Howard Mar 10 '13 at 06:07
  • 1
    [Please, don't use `mysql_*` functions,](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) [they are officially deprecated.](http://php.net/manual/en/function.mysql-connect.php#function.mysql-connect-refsynopsisdiv) – A. Rodas Mar 10 '13 at 06: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](http://php.net/manual/en/control-structures.while.php) loops if you need to split up your code into several php code snippets. – Aiias Mar 10 '13 at 06: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. – Andy Lester Mar 10 '13 at 06:09
  • @AndyLester It means i am getting the error "Error, insert query failed" – Harrison Howard Mar 10 '13 at 06:13
  • @A.Rodas I am using on my localhost for now and i use the newest for online sites. I have no clue what that has to do with anything. – Harrison Howard Mar 10 '13 at 06:13
  • Your "Error, insert query failed" doesn't help us at all, because it's a message you made up, never mind that the query that failed is a select, not an insert. You need to print the result of a call to mysql_error instead of making up your own. – Andy Lester Mar 10 '13 at 06:15
  • @AndyLester Okay how do i do that? – Harrison Howard Mar 10 '13 at 06:17
  • `$result=mysql_query($query) or die mysql_error();` It sounds like you don't know any programming at all, and you're just modifying something that somebody else gave you. That's pretty dangerous. – Andy Lester Mar 10 '13 at 06:20
  • What do you mean it works on localhost and not on php? do you mean it works on localhost and when you upload it online it doesnt work? – Muhammad Nasir Mar 10 '13 at 06:23
  • @MuhammadNasir What does that have to do with any question of mine? Sorry, but it just works online, but not on my local host machine. IDK why – Harrison Howard Mar 10 '13 at 06:25
  • @AndyLester All im getting is: Parse error: syntax error, unexpected T_STRING in /Applications/XAMPP/xamppfiles/htdocs/chatterr/new/core/dbconnect.php on line 7 – Harrison Howard Mar 10 '13 at 06:25
  • @HarrisonHoward: My mistake, put the call to `mysql_error()` in parens in the call to die: `die(mysql_error())`. Beyond that, I'm backing out of this. – Andy Lester Mar 10 '13 at 06:29
  • @AndyLester Please help me solve this, all the error that I am getting is "No database selected" – Harrison Howard Mar 10 '13 at 06:32
  • @AndyLester And i have selected one. – Harrison Howard Mar 10 '13 at 06:33

2 Answers2

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.

peterm
  • 91,357
  • 15
  • 148
  • 157
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"];
}
?>
Dead Man
  • 2,880
  • 23
  • 37