PHP not finding mySQL database? - php

I must be doing something wrong
I have a very simple script and a very simple database
No idea why it's not working..
Please help
<?php
error_reporting(E_ALL);
$link = mysql_connect('localhost', 'root', 'password');
if(!$link)
{
die('Could not connect: ' . mysql_error());
}
$database = mysql_select_db('test_db', $link);
if(!$database)
{
die('Could not connect to database: ' . mysql_error());
}
$result = mysqli_query($link, "SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysqli_fetch_array($result);
echo $row[0];
?>
It's not even giving any errors, just echoing 'Problem'...
The database connects fine, and there is 1 user in the database with an ID of 1 and forename is Cristian.

In $result = mysqli_query try just useing $result = mysql_query same in $row = mysqli_fetch_array should help
Try replaceing you code from $result onward with this
$result = mysql_query("SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysql_fetch_array($result);
echo $row[0];
?>

Related

Login not checking if password is wrong

So for some reason if the password is correct it knows and takes the user to the correct user account, but if the pass is wrong, it wont log them in but still takes them to the account page that isn't logged in.
Can someone please help me out to not re-direct them if the password is wrong
<?php
session_start();
//$connection = mysqli_connect('localhost', 'root', '');
$connection = mysqli_connect("pdb18.awardspace.net","*****","******","*****");
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, '******');
if (!$select_db)
{
die("Database Selection Failed" . mysqli_error($connection));
}
$username=trim($_POST['username']);
$password=trim($_POST['password']);
//$encoded_password = base64_encode($password);
$sql = "SELECT * from register where Username='".$username."' and Password='".$password."'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
$result = $con->query($sql);
$count = mysqli_num_rows($result);
//echo $count;
if ($count == 1){
while($row = $result->fetch_assoc()) {
$id=$row['id'];
}
$_SESSION['User'] = $username;
$_SESSION['UserId'] = $id;
echo "valid";
}
else{
echo "Invalid";
}
?>
Remove this line:
$result = $con->query($sql);
You are using procedural functions, mysqli_*.
This part of code $con->query is OOP style, which you are not using in your code, and overwritting the value o $result variable.
You can use both styles, but you should use the same connection, or $connection in your case.

PHP: Warning: mysql_fetch_array() expects parameter 1 to be resource

I got this error while adding this code. Would appreciate some help. It's for a CS jackpot site.
$sitename = "website.com"; // YOUR DOMAIN
$link = mysql_connect("localhost", "db_user", "db_pass"); // MYSQL , LOCALHOOST , USERNAME , PASSWORD
$db_selected = mysql_select_db('db_name', $link); // MYSQL DATABASE
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
while($row = mysql_fetch_assoc($query))
return $row[$rowname];
}
Some tips:
Use mysqli better than mysql
Split the vars in the query, like "SELECT ".$rowname." FROM ".$tablename;
Hope this help...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('host','username','password','database_name');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Select Query
$result = $mysqli->query("SELECT id, product_name FROM products");
while($row = $results->fetch_assoc()) {
echo $row["id"].' - '.$row["product_name"].'<br>';
}
$results->free();
$mysqli->close();

mysql_fetch_array to pull values into a variable

I know that this is dozy, but getting to the end of my tether, this should work, but I can't see why I can't place the first value of my order table into the variable order_id, can anyone see my mistake?
Thanks
$con = mysql_connect("localhost", "tim", "password");
if (!$con) {
die('Could not connect: '.mysql_error());
}
$sql = "SELECT TOP 1 order_order_id FROM tbl_order ORDER BY order_id DESC";
$result = mysql_query($sql, $con);
while ($rows = mysql_fetch_array($result)) {
$order_id = $rows;
}
Use this code and let me know if it works:
$con = mysql_connect("localhost","tim","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT TOP 1 order_order_id FROM tbl_order ORDER BY order_id DESC";
$result = mysql_query($sql,$con);
if (mysql_num_rows($result)) {
$rows = mysql_fetch_assoc($result);
$order_id = $rows['order_order_id'];
} else {
die('No order ID found');
}

No database selected on PHP

I got this code to create a JSON file with the data I have in my database:
<?php
$connect = mysql_connect("localhost", "root", "123", "boxr") or die("Error connecting with the host.");
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
$response = array();
$users = array();
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$gamertag = $row['gamertag'];
$banned = $row['banned'];
$users[] = array('gamertag' => $gamertag, 'banned' => $banned);
}
$response['users'] = $users;
$fp = fopen('users.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
The problem is when I run the script it shows the following:
No database selected
I checked my code twice but I can't figure out why its telling me that.
You have to select the database name with:
mysql_select_db ('your_db_name');
Well you can do it like that:
$connect = mysql_connect("localhost", "root", "123") or die("Error connecting with the host.");
mysql_select_db("boxr", $connect);
And later, correct it:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($result))...
By:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($sql))...
If you are calling the function with the name $sql, you can't later put the name of $result.
mysql_* functions are deprecated but you could change your query
$sql = mysql_query("SELECT * FROM dbname.users") or die(mysql_error());
Use :
$db_selected = mysql_select_db('db_name', $connect);
if (!$db_selected) {
die ('Can\'t use db_name : ' . mysql_error());
}

MySQL Query and display not working?

My MySQL database is setup with the name "chatterr" with the table name, "maillist". I am currently using the php below:
<?php
mysql_connect("localhost","root","");
//query databae
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows) {
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result, $row, "first_name");
$last_name=mysql_result($result, $row, "last_name");
?>
<?php echo $id; ?>
<?php
$row++;
}
?>
It works on localhost but doesn't work in PHP. What's wrong with the code?
Select a database with mysql_select_db before querying it
mysql_connect("localhost","root","");
mysql_select_db("chatterr");
or specify the database name in the query
$query = "SELECT * FROM chatterr.students ORDER BY id DESC LIMIT 4";
UPDATE: Besides that your is connection probably failing. Change
mysql_connect("localhost","root","");
to
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
to see if that's the case.
UPDATE3: Put it all together. Although that code has a LOT room for improvement it works perfectly fine on my machine.
<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("chatterr", $db);
if (!$db_selected) {
die ('Could not select db: ' . mysql_error());
}
//query databae
$query = "SELECT * FROM test.students ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result,$row,"first_name");
$last_name=mysql_result($result,$row,"last_name"); ?>
<?php echo $id; ?>
<?php
$row++;
}
?>
And please, don't use mysql_* functions for new code. They are
deprecated. Use prepared
statements with either PDO
or MySQLi.
Just do it this way :
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chatterr",$con) or die(mysql_error());
//query database
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
}
?>

Categories