jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
The warning tells you what is wrong. You can then look at the manual and it will show you that the second parameter you passed to the function is incorrect.
-
Is this a suitable preg_match for checking an email?
jcbones replied to jamesjmann's topic in PHP Coding Help
Take your pick -
Have you tried to EXPLAIN the SELECT statement? Sometimes DISTINCT doesn't like to be run with an ORDER BY clause. EXPLAIN should tell if that is a problem.
-
Accepted strtotime() formats
-
Try it through simpleXML: $xml = new SimpleXMLElement('http://www.britishinternettv.co.uk/vlc.xml',NULL,TRUE); echo $xml->info[0]->url; I believe that is right.
-
There are 3 different commands that will get the result list from the resource. $query = "SELECT * FROM pet"; $result = mysql_query($query) or die ("Couldn't execute query"); //This returns the resource. //get the result list #1 (not recommended.) while($row = mysql_fetch_array($result)) { //function returns a numerical and an associative array. foreach($row as $key => $value) { //you will find that you have 2 of everything. echo $key . ': ' . $value . '<br />'; } } //get result list #2. while($row = mysql_fetch_row($result)) { //function returns a numerical array. foreach($row as $key => $value) { echo $key . ': ' . $value . '<br />'; } } //get result list #3 while($row = mysql_fetch_assoc($result)) { //function returns an associative array. foreach($row as $key => $value) { echo $key . ': ' . $value . '<br />'; } }
-
Change: $db = mysql_select_db('test',$connection) or die('Could not connect to database'); //to $db = mysql_select_db('test',$connection) or die('Could not connect to database: ' . mysql_error()); See what happens.
-
If you created the account in cpanel, did you then assign the account to the database? After "MySQL add users", make sure you go down the page, and "Add users to Database". Then make sure that your database name and the user name is listed in the "current database" table, around the middle of the page.
-
Rookie Alert! Inserting data with php script question
jcbones replied to simpson_121919's topic in PHP Coding Help
Try this: It doesn't fix anything, it only lets you know what the database thinks. <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $dbc = mysqli_connect('localhost', 'root', 'x', 'aliendatabase') or die('Error connecting to MySQL server: <br />' . mysqli_connect_error()); $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " . "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " . "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')"; $result = mysqli_query($dbc, $query) or die('Error querying database: <br />' . mysqli_error($dbc)); mysqli_close($dbc); echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> -
Is the extension lowercase or uppercase letters.
-
Do not use globals, unless they exist in the global scope by default. <?php function function_1() { $variable_1 = 'hello world'; return $variable_1 } function function_2($variable_1) { echo $variable_1 } echo function_2(function_1()); ?>
-
Do you ever round down, or leave the price as is? As in: 100 + 10% = 110 or 119
-
Keep receiving a failed to open stream (for image) error
jcbones replied to Dave2136's topic in PHP Coding Help
You need to add a lot of error checking before trying to move the file. If there was an error with the upload, then of course the file wouldn't exist in the tmp folder. Therefore, move_uploaded_file() would fail. Take a look at this upload function. It is old. function uploadImage($setWidth = 1000, $setHeight = 1000, $filepath = 'upload/') { $content = NULL; if($_FILES["file"]["size"] > 900000) { $error[] = "File is to large, submit a smaller image!"; } elseif(empty($_FILES["file"]["name"])) { $error[] = "No File Uploaded!"; } else { if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/x-png")) && ($_FILES["file"]["size"] < 900000)) { $image = str_replace(' ', '_', $_FILES['file']['name']); if ($_FILES["file"]["error"] > 0) { $error[] = "*Return Code: " . $_FILES["file"]["error"] . "<br />"; } elseif (file_exists($filepath . $image)) { $error[] = '*Image exists: ' . $image ; } else { $uploadedfile = $_FILES["file"]["tmp_name"]; $size = getimagesize($uploadedfile); $type = $size['mime']; $width = $size[0]; $height = $size[1]; $filename = $filepath.$image; if($height > $setHeight || $width > $setWidth) { $newwidth=$setWidth; $newheight=($height/$width)*$setWidth; $tmp=imagecreatetruecolor($newwidth,$newheight); if($size[2] == IMAGETYPE_GIF) { $src = imagecreatefromgif($uploadedfile); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagegif($tmp,$filename,100); } elseif($size[2] == IMAGETYPE_JPEG) { $src = imagecreatefromjpeg($uploadedfile); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagejpeg($tmp,$filename,100); } elseif($size[2] == IMAGETYPE_PNG) { $src = imagecreatefrompng($uploadedfile); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagepng($tmp,$filename,9); } imagedestroy($src); imagedestroy($tmp); return $image; } else { if(move_uploaded_file($uploadedfile, $filename)) { return $image; } $error[] = '*Failed during file movement!'; } } } else { $error[] = "*Invalid file"; } } return $error; } //call function: //using defaults $image = uploadImage(); //specify arguments $image = uploadImage(20,40,'images/upload'); if(!is_array($image)) { //do database insert; $image holds image name. } else { foreach($image as $error) { echo $error . '<br />'; } } -
$time = time(); $last_time = date('D M j H:i:s \G\M\TO Y',$time); //echo: Thu Mar 10 18:33:48 GMT-0300 2011 echo $last_time; $last_time_plus_30 = date('D M j H:i:s \G\M\TO Y',strtotime('+30 seconds',$time)); echo $last_time_plus_30; Will work even if there are a couple seconds execution time between the date() functions.
-
Do you have a auto_increment primary key? To get the last one from a generated, use: mysql_insert_id()
-
Something like: <?php session_start(); $dir = 'path/to/image/directory'; //<<<---- Path to your images, NO TRAILING SLASH. if(!isset($_SESSION['images'])) { //If the image array IS NOT saved in sessions if ($handle = opendir($dir)) { //Open the image directory. while (false !== ($file = readdir($handle))) { //while there are files to read. if($file != "." && $file != "..") { //if the file isn't current dir, or above dir. $parts = explode('.',$file); //split the file on a period. $c = count($parts); //count the parts array. $ext = $parts[$c-1]; //ext will reside as the last value of the array. if(strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif') { //if the ext is image type. $images[] = $file; //save it to the image array. } } } closedir($handle); //close the directory. } sort($images); $_SESSION['images'] = $images; //write the image array to a session variable. } //this closes the if block, the above code will only run when the page is first open. //Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for //this script to pick them up. if(!isset($_SESSION['nextimage'])) { //if next image is NOT in the session array. $next = $_SESSION['images'][1]; //next image will be the second image in the image array. $current = $_SESSION['images'][0]; //current image will be the first image in the array. } else { //if next image is in the session array. if(isset($_GET['next'])) { //and next is in the url bar. $current = $_SESSION['nextimage']; //current image is changed to the one held in Sessions nextimage. } elseif(isset($_GET['prev'])) { //or if prev is in the url bar, $current = $_SESSION['previousimage']; //we go to the previousimage in our sessions array. } } $keys = array_keys($_SESSION['images'],$current); //find our array key of the current pic. $n = $keys[0]; //the key will reside in the first place of the keys array. $next = (array_key_exists($n+1,$_SESSION['images'])) ? $_SESSION['images'][$n+1] : $_SESSION['images'][0]; //if the next array key exists in the images array, set the next image, if it don't the first image is next. $previous = (array_key_exists($n-1,$_SESSION['images'])) ? $_SESSION['images'][$n-1] : end($_SESSION['images']); //if the previous key exists in the images array, set the previous image to it, if it don't set previous to the last value of the image array. $_SESSION['nextimage'] = $next; //write the next to the session array. $_SESSION['previousimage'] = $previous; //write the previous to the session array. echo '<a href="?next=1"><img src="' . $dir . '/' . $current . '" alt="images" /></a><br />' . "\n"; // print the image element to the page. ?>
-
Try: $last_time = date('D M j H:i:s \G\M\TO Y',strtotime('+30 seconds')); //echo: Thu Mar 10 18:33:48 GMT-0300 2011
-
You should get a parse error, I forgot the semi-colon on $num = mysql_num_rows($result) Must add semi-colon on it. $num = mysql_num_rows($result);
-
mysql in while loop for a different mysql
jcbones replied to MadLittleMods's topic in PHP Coding Help
a.* refers to article_comments AS a. The a refers to this table throughout the query. Being that we are joining the same table to itself, we need to create this reference so MySQL will know which columns we are talking about. (reference the error, when I missed the table identifier on the article_id column). Next I use the individual columns in the b identifier so that I can re-name those columns for less ambiguous returns. So, this is how the logic of the query works. -
mysql in while loop for a different mysql
jcbones replied to MadLittleMods's topic in PHP Coding Help
Sure, any time you run mysql queries inside of another queries while loop, it will most likely hit a snag. So, by joining the table to itself, we can get all the info we need with one query. This does 2 things. 1. Keeps us from running into the coding nightmare that you encountered. 2. Is more efficient because we interact with the database with one query, instead of many. Commented code below. //We build our query, joining the table to itself on the reply id to the id. $query = "SELECT a.*, b.comment_date AS reply_date, b.username AS reply_name, b.comment AS reply, b.reply_id AS reply_to FROM article_comments AS a JOIN article_comments AS b ON (a.id = b.reply_id) WHERE a.article_id = '$article_to_show_id'"; $search_for_article = mysql_query($query) or die($query . ' has an error: <br />' . mysql_error()); //die if there is an sql error <- debugging. Change to trigger_error() for logging to the error file. $last_id = NULL; //define our last id variable. while($row_comment = mysql_fetch_array($search_for_article)) // query loop, only one needed. { if($last_id != NULL && ($last_id != $row_comment['id'])) { //because the original comment will be returned for every reply to it, we use our last_id to find out if we need to show the original comment. // format the submit updat date right $comment_date_edit = $row_comment['comment_date']; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment['comment_date'] = $comment_date_edit; echo ' <br> <br> COMMENT:<br> <div> By: ', $row_comment['username'] ,' on ', $row_comment['comment_date'] ,' </div> <div> ', $row_comment['comment'] ,' </div> '; $comment_count++; } if(!empty($row_comment['reply_to']) && $row_comment['reply_to'] == $row_comment['id']) { //we check to see if the reply_to column is empty, if it is, there is no reply to the comment. As an added check, we also make sure it matches the id. // format the submit updat date right $comment_date_edit = $row_two['reply_date']; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_two['reply_date'] = $comment_date_edit; echo ' <br> <br> COMMENT REPLY:<br> <div> By: ', $row_comment['reply_name'] ,' on ', $row_comment['reply_date'] ,' </div> <div> ', $row_comment['reply'] ,' </div> '; $comment_reply_count++; } $last_id = $row_comment['id']; //assign the id, to our last_id variable. This will decide if we need to show our original comment on the next loop. } Joining tables is very powerful in MySQL. Learning how to do this will help you greatly improve the efficiency of your code. MySQL JOIN -
mysql in while loop for a different mysql
jcbones replied to MadLittleMods's topic in PHP Coding Help
Easy fix. Change: WHERE article_id = '$article_to_show_id'"; to: WHERE a.article_id = '$article_to_show_id'"; Let me know if that fixed it. -
mysql in while loop for a different mysql
jcbones replied to MadLittleMods's topic in PHP Coding Help
If you had your error level set for development, you would see what the error was. change: or trigger_error($query . ' has an error: <br />' . mysql_error()); To: or die($query . ' has an error: <br />' . mysql_error()); This should give us valuable info. -
I think all you need to do is wrap your $cbanner variable in PHP tags, so the server can parse it. <img src="<?php echo $cbanner; ?>"> Of course, this is speculation. You didn't give a whole lot of code from your HTML page. I make this decision based on the source code of your linked page.
-
mysql in while loop for a different mysql
jcbones replied to MadLittleMods's topic in PHP Coding Help
Run this as a test, this means DO NOT OVERWRITE YOUR ORIGINAL script. // what article are we showing? $article_to_show_id = $_GET['article_id']; $active_is_set_text = "1"; // Active Column text that makes it okay to show // Finding the article $search_for_article = mysql_query("SELECT * FROM articles WHERE id = '$article_to_show_id' AND active = '$active_is_set_text'"); while($row = mysql_fetch_array($search_for_article)) { // format the last updated date right $update_date_edit = $row[update_date]; $update_date_edit = date('F j, Y \a\t h:ia', $update_date_edit); $row[update_date] = $update_date_edit; // format the submit updat date right $submit_date_edit = $row[submit_date]; $submit_date_edit = date('F j, Y \a\t h:ia', $submit_date_edit); $row[submit_date] = $submit_date_edit; echo ' <div> ', $row[title] ,' </div> <div> by: ', $row[author] ,' on ', $row[submit_date] ,' </div> <div> ', $row[content] ,' </div> <div> Last Updated: ', $row[update_date] ,' </div> <form action="article_reply.php" method="post"> <input type="hidden" name="article_id" value="', $row[id] ,'" /> <button name="article_reply" type="submit" value="submit">Reply</button> </form> '; } $comment_count = 0; $comment_reply_count = 0; // Finding all of the comments $query = "SELECT a.*, b.comment_date AS reply_date, b.username AS reply_name, b.comment AS reply, b.reply_id AS reply_to FROM article_comments AS a JOIN article_comments AS b ON (a.id = b.reply_id) WHERE article_id = '$article_to_show_id'"; $search_for_article = mysql_query($query) or trigger_error($query . ' has an error: <br />' . mysql_error()); $last_id = NULL; while($row_comment = mysql_fetch_array($search_for_article)) { if($last_id == NULL || ($last_id != $row_comment['id'])) { // format the submit updat date right $comment_date_edit = $row_comment['comment_date']; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment['comment_date'] = $comment_date_edit; echo ' <br> <br> COMMENT:<br> <div> By: ', $row_comment['username'] ,' on ', $row_comment['comment_date'] ,' </div> <div> ', $row_comment['comment'] ,' </div> '; $comment_count++; } if(!empty($row_comment['reply_to']) && $row_comment['reply_to'] == $row_comment['id']) { // format the submit updat date right $comment_date_edit = $row_comment['reply_date']; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment['reply_date'] = $comment_date_edit; echo ' <br> <br> COMMENT REPLY:<br> <div> By: ', $row_comment['reply_name'] ,' on ', $row_comment['reply_date'] ,' </div> <div> ', $row_comment['reply'] ,' </div> '; $comment_reply_count++; } $last_id = $row_comment['id']; }