Random row selection

i have a vehicle inventory system ,that you guys helped me fix, that I want to be able to pull 2 random vehicles from it to display on a sidebar to a website.

Basically I want it to just grab 2 random cars (Image, Year, Make, Model, and Price) and place them in a vertical line for dynamic sidebar data. I was wanting the images to be grabbed randomly every time the page is refreshed or every time it gets visited but I’m having a little trouble with this code as well.

Another thing is that the image in the database is only an image location pointer, pointing to folder on the web server that’s back one directory (…/admin/$imagevariable).

[php]<?php
$dbhost=“localhost”;
$dbname=“auto”;
$dbuser=“root”;
$dbpass="";

mysql_connect("$dbhost","$dbuser","$dbpass") or die("Failed to connect to mysql");
mysql_select_db("$dbname") or die("Failed to open database");

$sql="select * from veh";
$res=mysql_query($sql) or die("Cant execute query");
$num_rows=mysql_num_rows($res);
$row=rand(1,$num_rows);

$sql2="select * from veh where id > 1" . $row;
$result=mysql_query($sql2);

while($data=mysql_fetch_array($result))
{
  echo '<img src="../admin/ $data[11]">';
  echo "<p><b><u>"  . "Year: </b></u>" . $data[1] . 
	   "<p><b><u>"  . "Make: </b></u>" . $data[2] . 
	   "<p><b><u>"  . "Model: </b></u>" . $data[3] .
	   "<p><b><u>"  . "Price: </b></u>" . $data[7]

	   ;
}

mysql_close();
?>[/php]

So far this code will grab the text data, but not the image itself only the image path, and it also only repeats the same record over and over.

Any help would be greatly appreciated in helping me figure this little piece of code out :slight_smile:

The answer your looking for is right in front of you…

Look at these lines you did it right 4 times and wrong once.
[php] echo ‘’;
echo “

” . "Year: " . $data[1] .

” . "Make: " . $data[2] .

” . "Model: " . $data[3] .

” . "Price: " . $data[7]

	   ;

[/php]

[php] echo “<img src=”…/admin/".$data[11]."">";
[/php]

Some suggestions to improve your code :

  1. to get the number of records in your table, use SELECT count(*) FROM…
  2. to select a record, use SELECT * FROM … LIMIT" . $row . “,1”
  3. if the random number in $row corresponds to the very last record in your table you will get only one record, so, I suggest that you execute this select twice.
  4. It is not wise to use an id for the random selection : think to what will happen if the randomly determined id has been deleted, use the LIMIT clause above

Thank you for the help. as far as showing the image it still would not let me go up a directory or down but 1 directory. the code works this way for the image though.

[php]echo “<img src=“admin/”.$data[11].”" height = 150 width = 150 border = 2>";[/php]

but if I try this, it wont work.

[php][php]echo “<img src=“admin/images/right/”.$data[11].”" height = 150 width = 150 border = 2>";[/php][/php]

As for the only selecting 2 random files to be shown, that still doesnt work for some reason using this new code it wont run at all.

[php]$sql2=“select * from vLIMIT” . $row . “,1”[/php]

Thank you all for your help with this little issue, im sure it’s something small and simple im missing but if you guys wouldnt mind, again, pointing me in the right direction that would be greatly appreciated.

maybe try this
[php]echo “<img src=‘admin/images/right/’.$data[‘11’].’ height=‘150px’ width=‘150px’ border=‘2’>”;[/php]

Ithink he wants you to have 2 queries one that counts but you could just use max instead
the queries he suggested look something like this might need changes not tested
[php]
$query1 = mysql_query(“SELECT count(*) FROM veh”) or die(mysql_error());

$query2 = mysql_query("SELECT * FROM veh LIMIT “. $row. ,1”) or die(mysql_error());
[/php]

k, heres what I got so far with that:

[php]<?php
$dbhost=“localhost”;
$dbname=“auto”;
$dbuser=“root”;
$dbpass="";

mysql_connect("$dbhost","$dbuser","$dbpass") or die("Failed to connect to mysql");
mysql_select_db("$dbname") or die("Failed to open database");

$sql="select * from veh";
$res=mysql_query($sql) or die("Cant execute query");
$num_rows=mysql_num_rows($res);
$row=rand(1,$num_rows);

$query1 = mysql_query("SELECT count(*) FROM veh") or die(mysql_error());

$query2 = mysql_query("SELECT * FROM veh LIMIT ". $row. 1) or die(mysql_error());

$result=mysql_query($query1);
$result=mysql_query($query2);

while($data=mysql_fetch_array($result))
{
  echo "<table>";
  echo "<tr>";
  echo "<td><img src=\"admin/".$data[11]."\" height = 150 width = 150 border = 2></td>";
  echo "</tr>";
  echo "<td><b><u>"  . "Year  </u>:</b>  &nbsp;&nbsp;"  . $data[1] . 
	   "<p><b><u>"   . "Make  </u>:</b>  &nbsp;&nbsp;"  . $data[2] . 
	   "<p><b><u>"   . "Model </u>:</b>  &nbsp;&nbsp;"  . $data[3] .
	   "<p><b><u>"   . "Price </u>:</b>  &nbsp;&nbsp;"  . $data[7] 

	   ;
}

mysql_close();
?>[/php]

It’s obvious I put that together wrong as it does not work. throws these errors upon running:

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\random\index.php on line 30

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\random\index.php on line 31

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\random\index.php on line 33

I think that its just the way I have it placed or maybe just a syntax thing.

Ok, I fixed it. For anyone else interested in using this the working code is:

[php]<?php
$dbhost=“localhost”;
$dbname=“auto”;
$dbuser=“root”;
$dbpass="";

mysql_connect("$dbhost","$dbuser","$dbpass") or die("Failed to connect to mysql");
mysql_select_db("$dbname") or die("Failed to open database");

$sql="select * from veh";
$res=mysql_query($sql) or die("Cant execute query");
$num_rows=mysql_num_rows($res);
$row=rand(1,$num_rows);

$sql2="SELECT * FROM veh ORDER BY RAND() LIMIT 2";
$result=mysql_query($sql2);

while($data=mysql_fetch_array($result))
{
  echo "<table>";
  echo "<tr>";
  echo "<td><img src=\"admin/".$data[11]."\" height = 150 width = 150 border = 2></td>";
  echo "</tr>";
  echo "<td><b><u>"  . "Year  </u>:</b>  &nbsp;&nbsp;"  . $data[1] . 
	   "<p><b><u>"   . "Make  </u>:</b>  &nbsp;&nbsp;"  . $data[2] . 
	   "<p><b><u>"   . "Model </u>:</b>  &nbsp;&nbsp;"  . $data[3] .
	   "<p><b><u>"   . "Price </u>:</b>  &nbsp;&nbsp;"  . $data[7] 

	   ;
  echo "<br><br><br><br>";
}

mysql_close();
?>[/php]

Feel free to use it at will, though im sure it could be written a little better. and thanks everyone for your help with this. You guys are the best :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service