Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Creating tables for images in msql

Hey there, i am willing to create a table in mysql database for my pictures and some information about the picture. How can i pull that or is it possible to create a search with php which can search inside the code and img folders when prompted without using mysql. Thanks in advance for those who will help. I will be very grateful if i can have a way of

6 Answers

Kevin Korte
Kevin Korte
28,149 Points

Thanks Ted for reformatting.

MUZ140770 Selestino Gama the first thing I'd figure out is your mysql_connect. You're not connecting to your database. Says access denied. You'll need to make sure you pass valid user credentials with your connect request.

Kevin Korte
Kevin Korte
28,149 Points

Most commonly, you store the image in a folder on your server, and in your database store things about the image, like height, width, alt text, description, url location, and whatever else you need.

That is very correct. I may stand to b corrected

my php code is as follows;

<?php
$app->get('/search1', function() use($app){
  $app->render('search1.twig');

  $connect=mysql_connect("localhost","root","password"); // Establish a connection 
mysql_select_db('resorts_africa',$connect); // Name of your DB 

if(!$connect) // If connection not established 
    print 'Could not connect to the database'; // Show an error 

if(isset($_GET['search'])) // If it's submitted 
    { 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT column FROM table WHERE column LIKE '%".$inp."%' "; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); // If query fail, let me know the error 
    if(mysql_affected_rows()===0) // If no match found 
        echo "{$inp} is not in our database."; // Let me know it is'nt found in the table 
    else 
        { 
        echo "<p>{$inp} was successfully searched.</p>"; // Yes, the query worked 
        while($row=mysql_fetch_array($r)) // Loop through the query results 
            echo "{$row[0]}<br>"; // Show the results 
        } // End of the else statement 
    } // End of the if statement 

function Clean($str) // Clean my input 
{ 
    return mysql_real_escape_string(strip_tags(trim($sStr))); // Remove traces of injection 
}
})->name('search1');
?>

And if i run the search it gives me the following;

The application could not run because of the following error:

Details

Type: ErrorException Code: 2 Message: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) File: C:\xampp\htdocs\project_2\index.php Line: 53 Trace

#0 [internal function]: Slim\Slim::handleErrors(2, 'mysql_connect()...', 'C:\xampp\htdocs...', 53, Array) #1 C:\xampp\htdocs\project_2\index.php(53): mysql_connect('localhost', 'root', 'password') #2 [internal function]: {closure}() #3 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Route.php(468): call_user_func_array(Object(Closure), Array) #4 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Slim.php(1357): Slim\Route->dispatch() #5 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Middleware\Flash.php(85): Slim\Slim->call() #6 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call() #7 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Middleware\PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call() #8 C:\xampp\htdocs\project_2\vendor\slim\slim\Slim\Slim.php(1302): Slim\Middleware\PrettyExceptions->call() #9 C:\xampp\htdocs\project_2\index.php(127): Slim\Slim->run() #10 {main}

can you please advise me where i am doing it wrong.

Thanks

Edited for formatting

Thanks for the help, i will figure out why its not connecting.

Kevin Korte
Kevin Korte
28,149 Points

Are you using phpmyadmin to manage the database?

Yes,

Kevin Korte
Kevin Korte
28,149 Points

Okay, select the table, than on that page you should see a tab that says "Privileges", where you can create a new username and password, ensure that that user has full access to your app database (which I think that user will by default), and than create that user, and use those new user credentials to access your database in your app. See if that fixes it.

Thanks, that settles it