I am trying to connect to a mysql database having the name "_query".
It gives me the error "No database selected" when I run the following code:
mysql_select_db("_query", $con);
$sql = "SELECT * from mdl_sms_msg";
$result = mysql_query($sql) or die(mysql_error());
I have spent a lot of time on trying to fix this, please help.
I have this code right here:
$conn = db_connect();
$username = $_POST['username'];
$result = $conn->query("select * from where username='".$username"'");
if (!$result) throw new Exception ("Could not excecute query");
}
The error message that I'm having troubles with is saying there's an undefined variable in one of these lines. It's only saying this for the $result = $conn->query("select * from where username='".$username"'"); line even though everything seems to be defined. If anyone knows how to fix this error, please let me know!
Change the code to this:
$conn = db_connect();
$username = $_POST['username'];
$result = $conn->query("select * from table_name where username='".$username."'");
if (!$result) throw new Exception ("Could not excecute query");
}
You forgot the ending period after the username. As one of the comments above noted. You also need to specify a table name so you'll need to replace table_name with your table name.
I am having an issue with the following php/sql. I ran the sql in the mysql terminal and it worked perfectly. I also tried my connect statement alerting a string and it succesfully alert the string when connected to the db. There is some error within this code that is returning null when it should be returning two integers. Any suggestions?
$xmax = mysql_fetch_row(mysql_query('SELECT MAX(x) AS xmax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0'));
$xmax = $xmax['xmax'];
$ymax = mysql_fetch_row(mysql_query('SELECT MAX(y) AS ymax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0'));
$ymax = $ymax['ymax'];
echo '<script>alert("'.$xmax.$ymax.'")</script>';
We can't see the rest of your code, but, you can refine this into one query, doing something like:
$connection = mysql_connect(
$DB_hostname,
$DB_username,
$DB_password) or die(mysql_error());
mysql_select_db($DB_name, $connection);
$query = mysql_query("
SELECT MAX(x) AS xmax, MAX(y) AS ymax
FROM headerfooter
WHERE site = 'Brighter_Vista' AND location = '0'
", $connection) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
$xmax = $row['xmax'];
$ymax = $row['ymax'];
}
echo '<script>alert("'.$xmax.$ymax.'")</script>';
However, if you have the time, ability and wish to learn, you should (as noted by others) look at and using either mysqli or PDO. Wise advice and in time you'll understand why.
The easist solution is to check if the sql statement is returning an error using:
mysql_query('SELECT MAX(y) AS ymax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0') or die('Error: '.mysql_error());
Side note: Don't use mysql_* functions anymore, that time has passed. Use either mysqli or PDO
I am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?
In db connection.php you must use a user name with password who is allowed to do operations on db.
Here is my error:
Warning: mysql_query() expects parameter 2 to be resource, null given...
This refers to line 23 of my code which is:
$result = mysql_query($sql, $connection)
My entire query code looks like this:
$query = "SELECT * from users WHERE userid='".intval( $_SESSION['SESS_USERID'] )."'";
$result = mysql_query($query, $connection)
or die ("Couldn't perform query $query <br />".mysql_error());
$row = mysql_fetch_array($result);
I don't have a clue what has happpened here. All I wanted to do was to have the value of the users 'fullname' displayed in the header section of my web page. So I am outputting this code immediately after to try and achieve this:
echo 'Hello '; echo $row['fullname'];
Before this change, I had it working perfectly, where the session variable of fullname was echoed $_SESSION['SESS_NAME']. However, because my user can update their information (including their name), I wanted the name displayed in the header to be updated accordingly, and not displaying the session value.
Your $connection variable is NULL that's what your error message is referring to.
Reason being is that you have not called mysql_connect. Once called it will assign you a resource where you can set it to the $connection variable, thus being non-null.
As an example:
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');
// now $connection has a resource that you can pass to mysql_query
$query = "SELECT * from users WHERE userid='".
intval( $_SESSION['SESS_USERID'] )."'";
$result = mysql_query($query, $connection)
include the mysql connections on your class file, for example:
connections/mysql.php
<?
$hostname_MySQL = "localhost";
$database_MySQL = "database";
$username_MySQL = "user";
$password_MySQL = "password";
$MySQL = mysql_pconnect($hostname_MySQL, $username_MySQL, $password_MySQL) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_MySQL,$MySQL);
?>
class.php
<?
include "Connections/MySQL.php";
class utils {
public function myFunction()
{
global $MySQL;
$sql = "select * from table";
$rs = mysql_query($sql, $MySQL) or die(mysql_error());
$filas = mysql_fetch_assoc($rs);
$totalFilas = mysql_num_rows($rs);
...
}
}
?>
You have two ways of doing this, you need to use mysql_connect to connect to your database, you can pass this to mysql_query if you desire, if you don't pass anything to mysql_query PHP uses the last link opened from mysql_connect
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
Have you connected to your database? If so please show this code too.
For now, just try removing the $connection variable, like this:
$result = mysql_query($query);
And see where that gets you.
$connection is assigned the value of the database connection resource id. You don't have that in your script, so the value of $connection is NULL, and that is why you are getting the error. You need to connect to the database before using mysql_query(). You should be okay after that.
You need to do:
$connection=mysql_connect('host','user','pass');
if($connection === false) {
echo "Error in connection mysql_error()";
}