mysql_connect.. no connection and no error - php

This seems like it should be so easy but I am not getting an error or any result:
$connection = mysql_connect("phpexamplesite.db.4211592.hostedresource.com", "username", "password");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
//2. Select a database to use
$db_select = mysql_select_db("username", $connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
Any ideas?
THanks:)
sorry about that... I did put the query in:
// 3. Perform database query
$result = mysql_query("SELECT * FROM subjects", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use returned data
while($row = mysql_fetch_array($result)) {
echo $row["menu_name"]. " ".$row["position"]. "<br />";
}
?>
<?php
//5. Close connection
mysql_close($connection);
?>
I'm not getting any error or results on the page...

You probably just don't have any result from the select statement.

It could be from a syntax error. You are missing a closing brace in the first code snippet in
if(!$db_select)
{
die("Database selection failed: " . mysql_error()); `
**}**
Try running your file from the command line through php test.php
Some servers (Apache) are configured to not output anything if there is a syntax error.

Related

Show all tables within given MySQL database

I'm trying to show all tables within a given MySQL database with php.
I'm very new to all this though and can't find a solution for it. Keeps giving an error 'no found file or directory'.
Anyone who can point out my mistakes here please?
Much appreciated!
<?php include "../inc/dbinfo.inc"; ?>
<html>
<body>
<h2>LIST TABLES FROM DATABASE</h2>
<?php
// Create connection
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
echo "Connection established with the database! </br>";
// SQL to show tables
$sql = "SHOW TABLES FROM paperlessdb";
$result = mysql_query($sql);
if (!$result) {
echo "Database error, could not list tables.\n</br>";
echo 'MySQL error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}
mysql_free_result($result);
?>
First make up your mind, either use mysqli procedural or object orientated. Not a combination of both because its confusing. To avoid that all together use pdo instead.
Now properly connect to the database, you can select the database when connecting to it automatically:
const DB_DATABASE = 'paperlessdb';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
if($result = $conn->query('SHOW TABLES')){
while($row = $conn->fetch_array($result)){
$tables[] = $row[0];
}
}
print_r($tables);
Use below query,
$sql = "SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'paperlessdb'";
We are fetching the data from information_schema db which stores the meta data about our database.
You are using mysqli to connect to the database but use the depreciated mysql to query the database.
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)){}
mysql_free_result($result);
You should use mysqli_query() and mysqli_fetch_array() instead.
It'a a bit more complex but mysql is decrecated and remove as PHP 7 so no choice to jump ahead. Check out PDO ass well. I personally go for mysqli but most say pdo is more intuitive.
It should look more something like:
$result = mysqli_query($conn,$sql);
if(!$result){
die('MySQL error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}

Error querying SQL SELECT in PHP

I am trying to connect to and select data from an PostgreSQL server. I am able to connect to the server but my select query appears to be running an error. Any suggestions?
<?php
$conn = "host=#### port=5432 dbname=consolidated user=#### password=####";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$dbconn = pg_connect($conn);
$result = pg_query($dbconn, "SELECT id FROM retailer_retailer;");
if (!$result) {
echo "An error occurred.\n";
exit;
}
while ($row = pg_fetch_row($result)) {
echo "ID: $row[0]";
echo "<br />\n";
}
?>
you miss the schema name right here, I assume you have table in public schema and your query should like-
$result = pg_query($dbconn, "SELECT id FROM public.retailer_retailer;");
If you have another schema then you can replace public with other schema name

Calling MySQL Stored Procedure through PHP

I've installed XAMPP and have been attempting to interrogate my database using PHP although I keep getting the same error.
<?php
$servername = "localhost";
$username = "root";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"use edgeserver; call ShowAll") or die("Query fail: " . mysqli_error());
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
?>
However this presents me with the following error:
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\amit\ShowAll.php on line 16
Query fail:
Mysql_error requires db connection to be passed. Try below:
mysqli_error($conn);
Thanks, it ended up being a combination of what you both said:
<?php
// Create connection
$conn = new mysqli('localhost', 'root', 'secret', 'edgeserver');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"call ShowAll") or die("Query fail: " . mysqli_error($conn));
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
Great response, hopefully this will be the base to me developing some good code.
I think the problem is from use edgeserver; call ShowAll.
use edgeserver, where edgeserver is the database that you want to use.
call ShowAll - what you try to accomplish with this?
Your new mysqli must be like this:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
source: http://php.net/manual/ro/mysqli.construct.php
And on mysqli_query you must do the queries like "Select * from table".

My SQL Query does not work, but returns no error

I'm trying to use a UPDATE SQL query in a PHP script but the query always returns a null or empty error. I am not sure why this is happening. Here is my code so far:
$conn1 = mysql_connect($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
$query = "UPDATE Products SET FilePrice=".$ourprice." WHERE FileID=".$id;
$sql = mysql_query($query);
if ($sql === TRUE) {
echo "Price Modified Successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn1->error;
}
Every time I run the script it just echoes Error:
Does anybody know why this is happening (I do not get a connection error initially either)?
The mysql API does not offer OOP syntax so your code is incorrect and will not report an error (actually, if you had error reporting set to display all errors PHP would have thrown an error). Combine that and your use of four parameterrs in mysql_connect and it looks you used mysql functions after reading a mysqli tutorial.
$conn1 = mysql_connect($servername, $username, $password);
if (!$conn1 ) {
die("Connection failed: " . $conn1->connect_error);
}
$db_selected = mysql_select_db($dbname, $conn1 );
if (!$db_selected) {
die ("Can't use foo : " . mysql_error());
}
$query = "UPDATE Products SET FilePrice=".$ourprice." WHERE FileID=".$id;
$sql = mysql_query($query);
if ($sql === TRUE) {
echo "Price Modified Successfully";
} else {
echo "Error: " . $sql . "<br>" . mysql_error();
}
This doesn't fix your UPDATE issue but will tell you what error MySQL is reporting.
(Your error is probably due to $ourprice being empty so check that next).

php my admin: no database selected

I am trying to learn php and mysql. So i tried making a database using phpmyadmin and connect it with my php.
Here is a simple example where I try to see if the database is working
<?php
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}
?>
<html>
<head>
<title>Databases</title>
</head>
<body>
<?php
$result = mysql_query("SELECT * FROM nameofdatabasetable", $connection);
if (!$result) {
die("Database query failed::: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row[1];
}
?>
</body>
</html>
<?php
mysql_close($connection);
?>
and i get
Database query failed::: No database selected
which means than this part of code
<?php
$result = mysql_query("SELECT * FROM users", $connection);
if (!$result) {
die("Database query failed::: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row[1];
}
?>
is not working (i put a different number of these ":" in each if.
Any help would be appreciated! Thank you!
The logic for your code doesn't make sense because if the connection doesn't happen then you would not be able to select a database and your database select statement is within the logic for if you cannot connect to the database. Try this instead:
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}else{
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}
I have to pass the username and password in the mysql_connect call. Here is my database open() function.
$this->con_error = "";
$db_con= mysql_connect($this->server, $this->username, $this->password);
if (!$db_con)
{
$this->con_error = mysql_error();
return false;
}
if(!mysql_select_db($this->database))
{
$this->con_error = mysql_error();
return false;
}
return $db_con;
die("Database connection failed: " . mysql_error());
$db_select = mysql_select_db("nameofdatabase",$connection);
mysql_select_db cannot possibly run here. It's only called after die.
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
Also it would be worth checking if you entered a password to your server.
$connection = mysql_connect("localhost","root"); (Missing Password)
The following code will trap any other error that might be happening and like the answer below this will provide mysql conectivity errors in case there is any.
try
{
$connection = mysql_connect("localhost","root", "password");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}catch (Exception $e){
error_log(" DB Error: ".$e->getMessage());
}

Categories