Solved

When trying to connect to database get error message

Posted on 2013-12-05
18
1,424 Views
Last Modified: 2013-12-05
$link = mysql_connect('localhost', 'root', 'root');

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25
0
Comment
Question by:burnedfaceless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698597
What is the code at line 25?
0
 

Author Comment

by:burnedfaceless
ID: 39698612
$link = mysql_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698621
Sorry but the error says mysqli_connect and that line says mysql_connect: are we missing something here?
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39698629
Please follow the guidelines in this article.  Choose MySQLi or PDO.  Don't use MySQL any more.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:burnedfaceless
ID: 39698658
Thanks Ray.

I'm still getting the following error and I thought maybe my syntax was wrong.

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

$link = mysqli_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698692
Except usually one has to declare the database and since probably you're using select_db function, your syntax is right. I presume you'reading the last edition of the Yank's book, since I have the code of an older edition and in chapter 4 there is no index.php. Can you post the index.php whole code?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39698693
0
 

Author Comment

by:burnedfaceless
ID: 39698753
Recently on my mac. I have the fourth edition. I can access the database through the terminal.

<?php
if (get_magic_quotes_gpc())
{
      function stripslashes_deep($value)
      {
            $value = is_array($value) ?
                        array_map('stripslashes_deep', $value) :
                        stripslashes($value);

            return $value;
      }

      $_POST = array_map('stripslashes_deep', $_POST);
      $_GET = array_map('stripslashes_deep', $_GET);
      $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
      $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if (isset($_GET['addjoke']))
{
      include 'form.html.php';
      exit();
}

$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
      $error = 'Unable to connect to the database server.';
      include 'error.html.php';
      exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
      $output = 'Unable to set database connection encoding.';
      include 'output.html.php';
      exit();
}

if (!mysqli_select_db($link, 'ijdb'))
{
      $error = 'Unable to locate the joke database.';
      include 'error.html.php';
      exit();
}

if (isset($_POST['joketext']))
{
      $joketext = mysqli_real_escape_string($link, $_POST['joketext']);
      $sql = 'INSERT INTO joke SET
                  joketext="' . $joketext . '",
                  jokedate=CURDATE()';
      if (!mysqli_query($link, $sql))
      {
            $error = 'Error adding submitted joke: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
      }

      header('Location: .');
      exit();
}

$result = mysqli_query($link, 'SELECT joketext FROM joke');
if (!$result)
{
      $error = 'Error fetching jokes: ' . mysqli_error($link);
      include 'error.html.php';
      exit();
}

while ($row = mysqli_fetch_array($result))
{
      $jokes[] = $row['joketext'];
}

include 'jokes.html.php';
?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698761
Please check the path of included file 'form.html.php'
0
 

Author Comment

by:burnedfaceless
ID: 39698803
It's in the same directory.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698839
So your directory tree is something like:

/Library/
    WebServer/
        Documents/
            yank/
                chapter4/
                    addjoke/
                        index.php
                        form.html.php
                        output.html.php
                        error.html.php

It is right?
I'm sorry for this inquiry, but I don't see errors in the script and I'm trying to guess what's going wrong...
0
 

Author Comment

by:burnedfaceless
ID: 39698896
yes sir

documents is "local host"
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698939
Mmmmh: in the code of the old edition I read, Yank named his file jokes.php, not jokes.html.php: have you renamed that?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39698955
From http://us3.php.net/manual/en/mysqli.construct.php ...

If you do Not have a default database set up in 'php.ini', the statement is expecting the name of the database to be the fourth parameter.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
0
 

Author Comment

by:burnedfaceless
ID: 39699091
Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

Warning: mysqli_set_charset(): Couldn't fetch mysqli in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 34

Warning: include(output.html.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

Warning: include(): Failed opening 'output.html.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

I hate to say it but should I just work through this book from an actual server rather than my computer?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39699129
Well, that could be a workaround: personally, I leaved localhost since I got several problem when I uploaded my sites/scripts on the server and I hade to adjust paths, external libs and so on: so now I work always on my web server set as production server will be.

But this wouldn't explain whay you're getting all those errors. It really seems something wrong in your localhost paths. Anyway, differently from I thought (comment ID 39698692), you never use select_db function so you should add your database name to your connection line:

$link = mysqli_connect('localhost', 'root', 'password', 'database_name');

I'm sorry I can't give you a better help
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39699180
Thanks for points, but I fear they are undeserved: what is the solution?
0
 

Author Comment

by:burnedfaceless
ID: 39699242
Use the database on the web server.

It will be less work in the long run.
0

Featured Post

Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question