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.
Related
I have simple msql based glossary - which stop working after upgrade from php5.4 to php 5.6.28
In form i send a variable to address like page.php?word=xxx
in page.php it should get $word from table.word with table.definition
Connection with database is working.
Error log looks
array(1) { ["word"]=string(5) "xxx" } bool(true)
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given
my code is
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$configfile = "config.php";
require $configfile;
$db = mysqli_connect("$host", "$username", "$password") or die ("could not connect to mysql");
mysqli_select_db($db, "$databasename") or die ("no database");
mysqli_query($db, "SET NAMES 'utf8' COLLATE 'utf8_polish_ci'");
function mysqli_result($db,$row=0,$col=0){
$numrows = mysqli_num_rows($db);
if ($numrows && $row <= ($numrows-1) && $row >=0){
mysqli_data_seek($db,$row);
$resrow = (is_numeric($col)) ? mysqli_fetch_row($db) : mysqli_fetch_assoc($db);
if (isset($resrow[$col])){
return $resrow[$col];
}
}
return false;
}
$word = "";
var_dump($_GET);
var_dump(isset($word));
if($word)
{
$getWord=mysqli_query($db, "SELECT word,definition FROM mdglossary WHERE word LIKE '$word' ORDER BY word");
}
else {....}
if($getWordArray=mysqli_fetch_array($getWord, MYSQLI_BOTH))
{
do
{
echo "....";
}
while($getWordArray=mysqli_fetch_array($getWord));
}
else {....}
I try to find solution "hundret" times by Internet and this page, but I just can't understand what happened and why it doesn't work at all.
You have set word to equal nothing.
$word = "";
then your sql will look like this
SELECT word,definition FROM mdglossary WHERE word LIKE '' ORDER BY word
So your looking for a word column with nothing in it. Is that your intention?
The error occurs like that, when i try to connect to DB to search something, it doesn't connects... here is the code below
$connection = new mysqli($db_server,$db_user,$db_pass,$db_name);
if($connection->connect_errno)
{
die ("Connection Failed");
}
$selcap = "SELECT * FROM captcha_codes ORDER BY RAND() LIMIT 1";
$seldcap = $connection->query($selcap);
if($seldcap === true)
{
while ($capdata = $seldcap->fetch_array(MYSQLI_BOTH))
{
$imglink = $capdata['image'];
$idcap = $capdata['id'];
$codecap = $capdata['code'];
}
} else {
$msgreg = "Couldn't connect to Captcha, please contact admin!";
}
The result is Couldn't connect to Captcha, please contact admin!
Use the less strict == comparison here: if($seldcap === true), that should solve the issue.
So the condition would look like: if($seldcap == true)
As mentioned by others, you could also check the number of results as well.
See the docs for mysqli::query() for more information on the results of that function.
If you want to check either query returns result or not use num_rows :
$seldcap = $connection->query($selcap);
if($seldcap->num_rows > 0)
{
//your while stuff
}
else{
//your error stuff
}
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
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?
All this section is supposed to do is collect an int from active and use it in the if statement to continue with the code. Can somebody please tell me why this is not working?
$act_qry = "Select active FROM user_m WHERE username = '$username' and password = '$Menrypted_password'";
$result_act = mysqli_query ( $connMS, $act_qry );
$value_act = mysqli_fetch_object($result_act);
if($value_act == 1)
{
//Do php stuff.
}
I am having this table in my database:-http://prntscr.com/9tnalx
Check this code:-
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
//connect to database
$link = mysqli_connect('localhost','root','','stack');
$act_qry = "Select product_id FROM bom WHERE bom_description = 'Table Tops'";
$result_act = mysqli_query ( $link, $act_qry ) or die(mysqli_error($link));
$value_act = mysqli_fetch_object($result_act);
if($value_act->product_id == 1)
{
echo $value_act->product_id;
}
// or you can do this
$value_act = mysqli_fetch_assoc($result_act);
if($value_act['product_id'] == 1)
{
echo $value_act->product_id;
}
mysqli_close($link);
?>
Output on my browser:- http://prntscr.com/9tnazj
Note:- I hope you can understand the code by checking my screenshot of table.Thanks