Mysql Error doesn't establish connection - php

I have a joint table query but keep getting errors as below:
I have also recreated the database again but still getting same error. Checked my connection to mysql and works fine - is there anything wrong in my joint table query?
$select_quote=mysql_query("SELECT authors.name, authors.id, authors.img, authors.slug, quotes.author_id, quotes.title, quotes.id, quotes.meta_keys, quotes.meta_description, quotes.slug, quotes.content
from quotes, authors
WHERE quotes.author_id = authors.id
ORDER BY RAND() LIMIT 1 ");
while ($row=mysql_fetch_array($select_quote)) {
$author_id = $row['authors.id'];
$author_name = $row['authors.name'];
$author_slug = $row['authors.slug'];
echo" $author_slug";
}
This is my connection
ob_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$user_name = "root";
$password = "root";
$database = "quotes";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
echo "Working";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
These are the errors I am getting
Warning: mysql_query(): A link to the server could not be established
in /home/user/public_html/index.php on line 6
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in /home/user/public_html/index.php on line 11
Could you please advice me on this?
Thanks

(To close this question)
You're closing your DB with mysql_close($db_handle); (you're telling it, if found then close DB connection) remove it or place it after successful query.
Plus, instead of $author_id = $row['authors.id']; do $author_id = $row['id']; and do the same for the rest.
Disclaimer: You should also consider switching to mysqli_ or PDO. mysql_ are deprecated and will be removed from future releases.

Related

Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 6

What's wrong in the code??
<?php
$host = "localhost";
$dbuser = "tesdb";
$dbpass = "123456";
$dbname = "tesdb";
// script koneksi php postgree
$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
//$query ="SELECT * FROM air_tanah.pembayaran";
$query ="select * from air_tanah.pembayaran";
$result = pg_query($dbcon, $query) or die('Query failed');
// output result
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo " Denda: " . $line['denda'] ." Penyimpan: " . $line['Penyimpan'] . "<br/>";
}
// free result
pg_free_result($result);
// close connection
pg_close($dbcon);
?>
and error like this
Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 12
Query failed
The code which you have entered does not allow us to really answer to your problem, we would need to know what is assigned to $dbcon variable.
pg_query expects there to be an instance of a Resource which holds the connection. Such resource is created using pg_connect or pg_pconnect method so we would need to see the contents of your db_con.php file: be sure to remove any credentials (hide them with * symbols for example).
It seems that something else actively sets the variable: are you sure that you have created a connection using pg_connect and not PDO for example (which would answer why you have there an object and not resource).

User co_p already has more than 'max_user_connections' active connections

I have a PHP Code to insert some information from a large text file
in the beginning of the file i have this code:
<?php
$host = "localhost";
$username = "user";
$password = "pass";
$db = "dbname";
mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_close;
set_time_limit(0);
$fh = fopen("file.txt", "r");
while ($row = fgets($fh)) {
//// CODE ....
mysql_query("INSERT IGNORE INTO `TABLE` VALUES (NULL, '$ETC', '$ETC', '$ETC')");
}
fclose($fh);
?>
but after some iserts i got this error :
Warning: mysql_connect(): User co_p already has more than 'max_user_connections' active connections in /home/username/public_html/file.php on line 7
User co_p already has more than 'max_user_connections' active connections
any solution for this problem ?
I'm the OWNER of the SERVER !
mysql_close;
should be generating an error.
This command should be
mysql_close();
Add error reporting to the
top of your file(s) while testing right after your opening PHP tag for example
<?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything.
But I have to ask, why are you connecting to the database and then closing the connection straight after?
After more code added.
As you are actually trying to access the database, you dont want to do a mysql_close(); where you have coded it.
But I have to add:
Every time you use the mysql_
database extension in new code
this happens
it is deprecated and has been for years and is gone for ever in PHP7.
If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements.
Start here
Try to close mysql connection:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
....
mysql_close($link);

Migrating to PHP7.x, Deprecated functions

So here's the problem, the script I purchase is written on PHP 5.x, and I'm using xampp with PHP7.x installed for development. Now I want to migrate my script to PHP7.x. Now I know this was asked a million times already but do you mind if you could take a look at my code and give your thoughts about it, or simply share your knowledge. I would deeply appreciate it.
Here is the code for my config.php
<?php
// mySQL information
$server = 'localhost'; // MySql server
$username = 'admin'; // MySql Username
$password = 'admin' ; // MySql Password
$database = 'arcade'; // MySql Database
// The following should not be edited
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$con = mysql_connect($server, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);
// Get settings
if (!isset($install)) {
$sql = mysql_query("SELECT * FROM ava_settings");
while ($get_setting = mysql_fetch_array($sql)) {
$setting[$get_setting['name']] = $get_setting['value'];
}
}
?>
The deprecated functions are:
mysql_connect()
mysql_error()
mysql_fetch_array()
mysql_query()
mysql_select_db()
Now, I don't want to use the PDO approach, I want to use mysqli instead. Am I suppose to just replace the mysql_* into mysqli_*? So it will become like these? I don't want to hide/surpress the deprecate warnings.
mysqli_connect()
mysqli_error()
mysqli_fetch_array()
mysqli_query()
mysqli_select_db()
I just offer you that migrate to PDO driver. Because every update you may see a lot of deprecation errors.
But if you can not do it the first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.
Note that, for some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
Look at difference between mysqli and mysql:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT ...");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
$mysql = mysql_connect("example.com", "user", "password");
mysql_select_db("test");
$res = mysql_query("SELECT ...", $mysql);
$row = mysql_fetch_assoc($res);
echo $row['_msg'];

PHP variables not printing to HTML webpage

We have the following code in the HTML of one of our webpages. We are trying to display all of the Wifi speeds and GPS locations in our database using the MySQL call and while loop shown in the below PHP code. However, it doesn't return anything to the page. We put echo statements in various parts of the PHP (ex. before the while loop, before the database stuff) and it doesn't even print those statements to the webpage.
<body>
<h2>WiFi Speeds in the System</h2>
<p>Speeds Recorded in the System</p>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbc = mysql_connect($hostname, $username, $password)
or die('Connection Error: ' . mysql_error());
mysql_select_db('createinsertdb', $dbc) or die('DB Selection Error' .mysql_error());
$data = "(SELECT Wifi_speed AND GPS_location FROM Instance)";
$results = mysql_query($data, $dbc);
while ($row = mysql_fetch_array($results)) {
echo $row['Wifi_speed'];
echo $row['GPS_location'];
}
?>
</body>
This line is incorrect, being the AND:
$data = "(SELECT Wifi_speed AND GPS_location FROM Instance)";
^^^
Change that to and using a comma as column selectors:
$data = "(SELECT Wifi_speed, GPS_location FROM Instance)";
However, you should remove the brackets from the query:
$data = "SELECT Wifi_speed, GPS_location FROM Instance";
Read up on SELECT: https://dev.mysql.com/doc/refman/5.0/en/select.html
Using:
$results = mysql_query($data, $dbc) or die(mysql_error());
would have signaled the syntax error. Yet you should use it during testing to see if there are in fact errors in your query.
Sidenote:
AND is used for a WHERE clause in a SELECT.
I.e.:
SELECT col FROM table WHERE col_x = 'something' AND col_y = 'something_else'
Or for UPDATE, i.e.:
UPDATE table SET col_x='$var1'
WHERE col_y='$var2'
AND col_z='$var3'
Footnotes:
Consider moving to mysqli with prepared statements, or PDO with prepared statements, as mysql_ functions are deprecated and will be removed from future PHP releases.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
"Thank you for the suggest but we tried that and it didn't change anything. – sichen"
You may find that you may not be able to use those functions after all. If that is the case, then you will need to switch over to either mysqli_ or PDO.
References:
MySQLi: http://php.net/manual/en/book.mysqli.php
PDO: http://php.net/manual/en/ref.pdo-mysql.php
hi mate i see some problem with your DB connection & query
here is example check this out
in SELECT is incorrect, being the AND .using a comma as column selectors:
and make condition for after set query & check data validation that is proper method
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "createinsertdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "SELECT `Wifi_speed `, `GPS_location `, FROM `Instance`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['Wifi_speed'];
echo $row['GPS_location'];
}
} else {
echo "0 results";
}
$conn->close();
?>

Parse error: syntax error, unexpected 'mysql_select_db' (T_STRING)- cannot see my error?

I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);

Categories