Jump to content

Choosing Random Website from database


Smudly

Recommended Posts

This code is supposed to choose a random website to be displayed once the user clicks the next button (This is for a traffic exchange project I'm working on). I'm getting the following errors:

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/s/m/u/smudlys/html/surf.php on line 10

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/s/m/u/smudlys/html/surf.php on line 10

Error in query: SELECT `users.id`, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active FROM users INNER JOIN websites ON websites.userid = users.id where websites.active='y' and users.credits > 0 and websites.userid != order by rand(). Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

 

Here is the code I'm using:

 

// Select a Random Website
$sitequery = "SELECT users.id, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active
FROM users INNER JOIN  websites ON websites.userid = users.id
where websites.active='y' and users.credits > 0 and websites.userid !=".$id." order by rand()";

$siteresult = mysql_query($sitequery) or die("Error in query: $sitequery. " . mysql_error());
if (mysql_num_rows($siteresult) != 0){
$siterow = mysql_fetch_array($siteresult);
$url = $siterow["url"];
}

 

any suggestions? Where did I go wrong?

Link to comment
https://forums.phpfreaks.com/topic/204311-choosing-random-website-from-database/
Share on other sites

In the include file I failed to put the <?php ?> inside. That is now fixed.

 

Here is my current error:

 

Error in query: SELECT users.id, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active FROM users INNER JOIN websites ON websites.userid = users.id where websites.active='y' and users.credits > 0 and websites.userid != order by rand(). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by rand()' at line 3

 

And my code:

<?php

session_start();

include('inc/connect.php');

// Select a Random Website
$sitequery = "SELECT users.id, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active
FROM users INNER JOIN websites ON websites.userid = users.id
where websites.active='y' and users.credits > 0 and websites.userid !=".$id." order by rand()";

$siteresult = mysql_query($sitequery) or die("Error in query: $sitequery. " . mysql_error());
if (mysql_num_rows($siteresult) != 0){
$siterow = mysql_fetch_array($siteresult);
$url = $siterow["url"];
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.