MySql error with a query - php

I'm writing a web application and now I have a problem
This is my database_connect.php:
<?php
$host = 'xy';
$user = 'xy';
$pass = 'xy';
$db = 'xy';
$dbconnect = mysql_connect($host, $user, $pass) or die("error");
mysql_select_db($db) or die("error");
?>
I am trying to establish connection to the database and insert some data with the following code:
<?
function addChapter(){
?>
<form method="post" action="">
<br>
<input name="chapter" type="text" value="Naslov">
<br><br>
<input type="submit" name="submit" value="Potrdi">
</form>
<?php
if( isset($_POST['submit']) ){
$chapter = mysql_real_escape_string($_POST['chapter']);
if( $_GET['stran'] == 'fizika' ){
$table = 'tblphysics';
}else if( $_GET['stran'] == 'kemija' ){
$table = 'tblchemistry';
}
$chapter_id_query = mysql_fetch_assoc( mysql_query("SELECT chapter_id FROM ".$table." ORDER BY chapter_id DESC LIMIT 1") );
$chapter_id = $chapter_id_query['chapter_id'] + 1;
if( ($chapter != "") && ($chapter_id != "") ){
$sql = "INSERT INTO ".$table." (chapter, version, chapter_id) VALUES ('$chapter', '1', '$chapter_id')";
$neki = mysql_query($sql, $dbconnect) or die("<p class=\"msg warning\">Napaka pri ustvarjanju poglavja.</p>");
echo '<p class=\"msg done\">Poglavje uspešno dodano.</p>';
//mysql_close($dbconnect);
}
}
}
?>
I am getting the following error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/a4896862/public_html/functions.php on line 81
Line 81 is
$neki = mysql_query($sql, $dbconnect) or die("<p class=\"msg warning\">Napaka pri ustvarjanju poglavja.</p>");
I know I should be using PDO or mysqli, but this will be just temporary, but it needs to work so i can continue
Anyone has any idea what is going wrong? It is causing no problem when reading from the database.

Try removing the $dbconnect argument, since the first query works as intended without it (did it ?).
$neki = mysql_query($sql) or die('...');
But, seriously, I doubt that solving issues in some outdated mess will be faster than starting database_connect.php all over. You will get much more quality answers if you use technologies that are still supported.

It seems that you don't include your database_connect.php in your second file (that contains the function). Try adding the following line before function addChapter() :
include 'database_connect.php'; // This is correct if the files are in the same directory...

Try removing the second paramater $dbconnect as it may not be in scope.
Does it work without it?

Related

Search function with php

I've got my code which is searching my database and table for a certain condition but when I search it doesn't return any result. I've looked at a few tutorials and cant find the issue. Any help is appreciated. I know the code is outdated and I should be using mysqli. I will be changing this when the issue is rectified.
<?php
$output = NULL;
if(isset($_POST['submit'])){
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("first_db") or die("can not connect");
$search = $mysql->real_escape_string($_POST['search']);
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name LIKE '%search%'");
if($resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$StaffStatus = $rows['StaffStatus'];
$name = $rows['Name'];
$output = "Staff Status: $StaffStatus<br/>name: $Name<br/><br/>";
}
}else{
$output = "No results";
}
}
?>
<form method-"POST">
<input type="TEXT" name"search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
Your query is written wrong
instead of
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name = LIKE '%search&'");
try this
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name LIKE '%search%'");
edit: added nogad's comment about changing the & to %
Try the query in phpmyadmin first. If there's an error in the query it will tell you

PHP mySQL xampp fatal error

When i run my php file with the following code:
<?php
//configurution of server
$serve = mysql_connect('localhost', 'root', 'password');
if (!$serve) { echo 'error' ; }
$db = mysql_select_db('record', $serve);
//action to include
if($_GET['action'] == 'include') {
$name = $_GET ['name'];
$lastname = $_GET['lastname'];
$SQL = "insert into user (name, lastname) VALUES ('$name', '$lastname')";
$re = mysql_query($SQL, $serve);
}
//action list
if($_GET['action'] == 'userlist') {
$SQL = "SELECT * FROM user";
$re = mysql_query($SQL, $serve);
$num = mysql_num_rows($re);
if($num > 0) {
//view screen
while ($Line = mysql_fetch_object($re)) {
echo "<b>Name: </b> {$Line->name} <b><br></b>
<b>Lastname: </b> {$Line->lastname} </br><hr>";
}
}
else {
echo 'no user recorded';
}
}
?>
This error appears:
Fatal error: Call to undefined function mysql_connect() in C:\xampp\htdocs\xampp\record\www\connect.php on line 5
I have ran the php file below to check my extension list to see if '[xx] => mysql' is displayed which it isn't. I have added 'extension=php_mysql.dll' to 'php.ini' and restarted apache & mysql but this still isn't working. I have also added 'C:\xampp\php\ext' into the 'path' environmental variable. I have checked the internet but can't seem to find a solution to my problem, can anyone please help me. Thank-you.
<?php // extensions_list.php
$list = get_loaded_extensions();
$list2 = array_map('strtolower',$list);
sort($list2);
echo '<pre>'.print_r($list2,true).'</pre>';
?>
mysql_xxx functions are deprecated, use mysqli_xxx or PDO instead.
http://php.net/manual/en/function.mysqli-connect.php
http://php.net/manual/en/book.pdo.php
Currently the use of mysql_connect function is not advisable because it is deprecated. Use mysqli or pdo instead.
For example:
$connection = new mysqli($dbhost,$username,$password,$dbname);
$query = "SAMPLE QUERY";
$connection->query($query);
or
Look here for PDO

SQL / PHP if NULL show else

We are trying to complete / fix the below code, We cant seem to do the following.
Check if 'Check_Activation' is set to 'NULL' within the Database
IF value is NULL direct the user to one of the forms (1,2,3)
And finally if the 'Check_Activation' has already been activated and isn't 'NULL' prevent user from accessing one of the 3 forms.
I know its basicly there but we can't seem to figure out the final bug.
Please have a quick look at the code below and if anyone notices anything that isn't right please advice us.
Paste Bucket / Version
Website URL
<?php
$username = $_POST['username'];
$activation_code = $_POST['activation_code'];
$activation_codeurl = $activation_code;
$usernameurl = $username;
$db_host = "localhost";
$db_name = "aardvark";
$db_use = "aardvark";
$db_pass = "aardvark";
$con = mysql_connect("localhost", $db_use, $db_pass);
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
$checkcustomer = mysql_query("SELECT `Check_Activation` FROM `members` WHERE `Username` = '".$username."' & `Activation` = '".$activation_code."'; ");
$array = mysql_fetch_array($checkcustomer);
if (is_null($array['Check_Activation'])) {
$username = substr($username, 0, 1);
if($username == '1') {
$redirect_url='form-one.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
} elseif($username == '2') {
$redirect_url='form-two.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
} elseif($username == '3') {
$redirect_url='form-three.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
}
header("Location:". $redirect_url);
}
else
{
?>
Try this, You need to fetch the row from table and then you can check the values,
$val = mysql_fetch_array($checkcustomer);
if (is_null($val['Check_Activation']))
instead of
$val = mysql_query($checkcustomer);
if ($val == 'NULL')
NOTE: Use mysqli_* functions or PDO instead of using mysql_* functions(deprecated)
before I get into the technicality of what your are trying to accomplish, I have some advice for your code in general. You should avoid using the mysql api as it is deprecated, and use the mysqli api instead. I think you will also find that it is easier to use.
Now for the code:
You have this line in your code which seems to be incorrect, $checkcustomer is a result set from your previous query, so why are you running it as a query again?
$val = mysql_query($checkcustomer);
You already have the result set so do this:
$array = mysql_fetch_array($checkcustomer);
And then take the value of Check_Aviation;
if (is_null($array['Check_Aviation'])) {
//Do Something
}
Should solve your issue

problem with fetching results from mysql via php function

can some one point out the problem with this code? It supposed to fetch data from mysql but it returns blank. Here is the full code.
<ul class="list">
<?php
require("object/db.class.php");
error_reporting(0);
function entry_tickets() {
if($_SESSION['dojopress_global:root'] == false) {
$entry_ticket .= <<<ENTRY_TICKET
<li><p><img src="http://cdn1.iconfinder.com/data/icons/humano2/32x32/apps/gnome-keyring-manager.png" />Access denied</p></li>
ENTRY_TICKET;
} elseif($_SESSION['dojopress_global:root'] == true) {
$q = "SELECT * FROM `notice` ORDER BY nid LIMIT 12 DESC";
$r = mysql_query($q);
if ( mysql_num_rows($r) == 0 ) {
$entry_ticket .= <<<ENTRY_TICKET
<li><p><img src="http://cdn1.iconfinder.com/data/icons/humano2/32x32/status/dialog-information.png" /> Nothing to display</p></li>
ENTRY_TICKET;
} elseif ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_array($r) ) {
$nid = stripslashes($a['nid']);
$note = stripslashes($a['note']);
$type = stripslashes($a['type']);
$private = stripslashes($a['private']);
$date = stripslashes($a['date']);
$author = stripslashes($a['author']);
function note_type($type) {
if($type == 1) { $type = "posted a comment!"; } elseif($type == 2) { $type = "raised a support ticket!"; } else { }
return ($type);
}
$entry_ticket .= <<<ENTRY_TICKET
<li><p> $athor, note_type($type)</p></li>
ENTRY_TICKET;
return $entry_ticket;
}
}
}
}
echo entry_tickets();
?>
</ul>
<div style="clear:both;height:10px;"></div>
sorry forgot db.class.php
<?php
session_start();
//connect.php
$host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "au";
$connectClass = mysql_connect("$host", "$db_user", "$db_pass") or die ("Couldn't establish connection to database server.");
$dbObject = mysql_select_db("$db_name", $connectClass) or die ("Couldn't select database.");
?>
error reporting disabled error code
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\wamp\www\ageis\note.php on line 22
Your mysql syntax looks bad. You have:
SELECT * FROM `notice` ORDER BY nid LIMIT 12 DESC
try
SELECT * FROM `notice` ORDER BY nid DESC LIMIT 12
Erik's comments should have pointed you in the right direction, however:
Figure out where PHP logs errors. Either turn up php's error reporting level so you see more errors, or start tailing your apache error_log
You should always check for errors after running mysql_query and do some sort of logging on failure. This is probably best accomplished by writing a wrapper function for mysql_query that does this, or using a 3rd party db wrapper that has already solved this problem.
You're redefining function note_type every time through your while loop. You have a return call outside the function, below it. Really I can't see how this is even syntactically correct. It looks like you have a large problem with mismatched brackets.
As for an actual database issue, you should check mysql_error() if no rows return from the call because it's likely something went wrong.
Also I recommend using PDO which is a true database class instead of the native function based ones.

live search with Jquery

I am trying to implement a live search on my site.
I am using a script somebody has already created. http://www.reynoldsftw.com/2009/03/live-mysql-database-search-with-jquery/
I have got the Jquery, css, html working correctly but am having troubles with the php.
I need to change it to contain my database information but everytime I do I recieve an error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\search.php on line 33
These are the details of my database:
database name: development
table name: links
Columns: id, sitename, siteurl, description, category
This is the php script
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password";
$dbname = "links";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
if(isset($_GET['query'])) { $query = $_GET['query']; } else { $query = ""; }
if(isset($_GET['type'])) { $type = $_GET['type']; } else { $query = "count"; }
if($type == "count")
{
$sql = mysql_query("SELECT count(url_id)
FROM urls
WHERE MATCH(url_url, url_title, url_desc)
AGAINST('$query' IN BOOLEAN MODE)");
$total = mysql_fetch_array($sql);
$num = $total[0];
echo $num;
}
if($type == "results")
{
$sql = mysql_query("SELECT url_url, url_title, url_desc
FROM urls
WHERE MATCH(url_url, url_title, url_desc)
AGAINST('$query' IN BOOLEAN MODE)");
while($array = mysql_fetch_array($sql)) {
$url_url = $array['url_url'];
$url_title = $array['url_title'];
$url_desc = $array['url_desc'];
echo "<div class=\"url-holder\">" . $url_title . "
<div class=\"url-desc\">" . $url_desc . "</div></div>";
}
}
mysql_close($conn);
?>
Can anybody help me input this database info correctly? I have tried many times but keep getting an error. Thanks in advance.
EDIT: IT IS CONNECTING TO THE DATABASE WITHOUT AN ERROR. CHECK HERE http://movieo.no-ip.org/
the mysql_query() call is failing an returning false instead of a resource. My bet is that mysql_select_db() is failing. This should show the error:
mysql_select_db($dbname) or die('Couldn\'t select DB: '.mysql_error());
Compare: database name: development to $dbname = "links";
I think you should change it to the right name.
As well as changing the $dbname to development, check your two SQL statements. They are selecting from the table urls instead of links.

Categories