php mysql database connection error - php

I have the following php code that gets user login details from a html form:
$con=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select username from users where user='$_POST[username]' limit1";
$result = mysql_query($query);
echo result;
But when I run it, i seem to be getting these errors:
Warning: mysql_query() [function.mysql-query]: Can't connect to local
MySQL server through socket '/directory omitted' (2) in
/directory omitted on line 10
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/directory omitted on line 10
Can anyone please help out? thanks very much!

You have mixed mysqli with mysql so there's a lot of typos
Code should be:
$con = mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$u = $_POST['username'];
$sql = "SELECT username FROM users WHERE user='$u' LIMIT 1";
$query = mysqli_query($con, $sql);
if ($row = mysqli_fetch_assoc($query)) {
echo $row['username'];
}
Hope it worked.
Or if you need all rows printed it should be:
while ($row = mysqli_fetch_assoc($query)) {
echo $row['username'];
}

$query = "select username from users where user='$_POST[username]' limit1";
$result = mysql_query($query);
These lines should be Like the following
$query = "SELECT username FROM users WHERE user='".$_POST['username']."' LIMIT 0,1";
$result = mysqli_query($con,$query);
print_r(mysqli_fetch_array($result));

Related

I'm trying to echo data from DB using MySql and php

I'm trying to echo data from DB but it is showing 500 error. Same code works in some other server but it is not working in my present diff server.
<?php
error_reporting(0);
session_start();
$hostname_dbConn = "localhost";
$database_dbConn = "data_data";
$username_dbConn = "user_data";
$password_dbConn = "datauser";
$dbConn = mysql_pconnect($hostname_dbConn, $username_dbConn, $password_dbConn) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($database_dbConn, $dbConn);
$query_getconfig = "SELECT * FROM configuration WHERE status = 'A'";
$getconfig = mysql_query($query_getconfig, $dbConn) or die(mysql_error());
$row_getconfig = mysql_fetch_assoc($getconfig); $totalRows_getconfig = mysql_num_rows($getconfig);
?>
Remove error_reporting(0); and check what errors exactly are you facing?
Either #mysql_pconnect($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia) or trigger_error(mysql_error(),E_USER_ERROR); //# infront of mysql_pconnect or use below written code
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DBNAME);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
For fetch record.
$sql= "SELECT * FROM configuration WHERE status = 'A'";
// get the result object.
$result = $mysqli->query($sql);
// fetch the result row.
$data = $result->fetch_assoc();
return $data;
with mysqli
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);

php results return empty

I am trying a connection to mysql and retrieve some information. The connection is successful, but produces a blank page. Below is the code. Appreciate help in checking what is wrong.
Thank you in advance
Richard
<html>
<body>
<?php
// include server parameters
require('includes/configure.php');
$con=mysqli_connect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD,DB_DATABASE);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$username = $_GET['username'];
//$password = $_GET['password'];
$result = mysqli_query($con,"SELECT * FROM Workshops");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
</body>
</html>
Before $row = mysqli_fetch_array($result);
add this lines to 'what you get from DB'
echo "<pre>".var_dump($result)."</pre>";
Looks like mysqli_fetch_array has two categories i.e. numeric and associative array. Take a look at the example in the link. You can change your code as shown below.
http://www.w3schools.com/php/func_mysqli_fetch_array.asp
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["yourtablecolumn"],$row["yourtablecolumn"]);
You can also use, mysqli_fetch_assoc($result).
$row = mysqli_fetch_assoc($result)
<?php
$connection = new mysqli ("localhost", "root", "root", "register"); //1. You have to change here
if ($connection -> connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$select_db = mysqli_select_db($connection, 'register'); //2. you have to change Database name
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
$query = "SELECT * FROM `Workshops`";
$result = mysqli_query($connection, $query)
or die(mysql_error());
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
?>
Check with this code again rewrote & tested
Note : I have marked the places where you should change according to your database configurations
Open for any modifications
This should be in comment but I don't have enough reputation to comment, so I just post as answer.
You can try these checking:
Make sure your table name is correct
Instead of $data = $row[0], try $data = $row['your_columm_name']. If you only want a single record, edit your query by adding WHERE clause.
Hope these help, good luck.

php query to database returning blank results

----- UPDATED
Code works when I'm running it from my schools public_html folder. However, when I create the exact same file, upload to my personal web server it does not work and only produces a blank screen. Not exactly sure what this means, maybe that I don't have permissions?
Updated Code to reflect suggestions (still not working)
-----------
Hi I'm a student learning to use php and postgres. Currently I'm just trying to open a connection from my hosted website to my database on my school's server. I'm getting a blank result with my following php file.
I put 'xxxxx' as the connection fields in order not to give away my username and password - the real file has the correct information.
My question is... is there anything wrong with this php file?
<html>
<body>
<h1>Test</h1>
<?php
$username = "xxxxx"
$password = "xxxxx";
$databasename = "xxxxx";
$hostname = "xxxxx";
$connection = pg_connect("host=$hostname dbname=$databasename user=$username password=$password") or die ("could not connect");
$query = "SELECT unit_name from units";
$result = pg_query($connection, $query) or die("ERROR: " . pg_last_error());
$row = 0;
while ($row = pg_fetch_row($result)) {
echo "$row[0]<br>\n"
};
pg_close($connection);
?>
</body>
</html>
You have to use pg_fetch_row
Try this
$query = "SELECT unit_name from units";
$result = pg_query($connection, $query) or die("ERROR: ".pg_last_error());
if(!result)
{
echo "An error occurred.\n";
exit;
}
$data = [];
while ($row = pg_fetch_row($result)) {
$data[] = $row;
}
foreach($data as $value)
{
echo $value;
}
This Code success on my side! try this.
$link = mysqli_connect("YourHostname", "Your Username", "YourPassword", "YourDBName");
// Check conn
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql="/*Your SQL CODE HERE*/";
if(mysqli_query($link, $sql)){
echo "Success!";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);

mysql php query dies

Im creating a login php script that connects to a mysql db with a table called users containing a list of users. I am running into what looks like either an empty set or a mysql error.
I have included a connection error echo and a query die echo and the script is echoing the latter error that gives me the query string. I run this exact query in phpMyAdmin and get the result I expect (it returns the user).
Why is it dying?
$cxn was defined on the login page that calls this posts to this page
$pass = addslashes($_POST['password']);
$email = addslashes($_POST['username']);
$pw = md5($_POST['password']);
$query = "SELECT * FROM `users` WHERE (`user_email`='$email' AND `user_pass`='$pw')" ;
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($cxn,$query) or die(mysqli_error($cxn)."Query= ".$query);
Ive left the first version up. Here is the current incarnation which has the same result.
<?php
$email = ($_POST['username']);
$pw = md5($_POST['password']);
$query = "SELECT * FROM `users` WHERE (`user_email`='$email' AND `user_pass`='$pw')" ;
if (mysqli_connect_errno($cxn)){ //remove later
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} //to here
$result = mysqli_query($cxn,$query) or die(mysqli_error($cxn)."Query=".$query);
echo '<hr>';
echo $result;
while($row = mysql_fetch_array($result)){
echo $row['user_email'];
echo '<hr>';
echo $row['user_pass'];
echo '<hr>';
echo $row['user_fname'];
echo '<hr>';
}
?>
Because your not passing $cxn variable while checking connection:
Change this:
if (mysqli_connect_errno()){
to:
if (mysqli_connect_errno($cxn)){
I just noticed this but you have the following:
$email = ($_POST['username']);
And this should be simply:
$email = $_POST['username'];
Also, try writing your query like this:
$query = "SELECT * FROM `users` WHERE `user_email` = '".$email."' AND `user_pass` = '".$pw."'";

php while loop error db

function DisplayPoints()
{
$con = mysql_connect("localhost", "grame2_admin", "password") ;
if (!$con) {
die("Can not connected: " . mysql_error());
}
mysql_select_db("grame2_webpage",$con);
$sql = "SELECT points FROM tablename WHERE username = $username";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
It echo out info, that there is error in line 164, LINE ABOVE THIS.
echo $record['points'] ;
}
mysql_close($con);
}
the main idea is to echo out INT from specific user in webpage. Please help! :)
Change
$sql = "SELECT points FROM tablename WHERE username = $username";
to
$sql = "SELECT points FROM tablename WHERE username = '$username'";
^ ^
On a side note use prepared statements with either mysqli or PDO. mysql extension is deprecated and is no longer supported.
UPDATE The other problem is that your $username variable is not initialized. You probably need to pass it to your function as a parameter
function DisplayPoints($username)
{
...
}
And when you call your function pass a value
DisplayPoints('user1');
or
$username = $_POST['username'];
// here should go code to validate and sanitize $username
DisplayPoints($username);

Categories