dtwy2858 2013-03-18 23:29
浏览 27
已采纳

MAMP:不能做MySQL查询

Some background: I initially tried doing Apache, MySql, and PHP on windows, each installed separately (what a PITA that was!). Eventually I heard that I would be better suited on the mac, so I got the MAMP.

Glad to find that everything has been installed, I try playing around with it. Basically everything works, except for MySQL queries (I am trying to insert data into a table).

Here's my code:

<h1>hello world</h1>
<?php
echo "hello ";
echo date('H:i:s, jS F Y');
$link = mysql_connect('localhost', 'root', 'root');
if ($link)
    echo "success";
else
    echo "failure";

$db_selected = mysql_select_db('MyDB', $link);
if ($db_selected) 
    echo "success";
else
    echo "failure";

$my_query = mysql_query($link,"INSERT INTO comments (cName, cComment)
VALUES ('Peter', 'Hellloooooo')");

if ($my_query)
    echo "success";
else
    echo "failure";

mysql_close($link);
?>

My output looks like the following:

hello world

hello 00:14:12, 19th March 2013successsuccessfailure

As you can see, mysql_connect and mysql_select_db work, but mysql_query does not. I've spent many fruitless hours trying to figure this out. What is wrong with the code?

  • 写回答

1条回答 默认 最新

  • doumei1926 2013-03-18 23:32
    关注

    Try

    $my_query = mysql_query("INSERT INTO comments (cName, cComment)
    VALUES ('Peter', 'Hellloooooo')", $link);
    

    Next time better mysqli or PDO.

    Updated: Mysqli: U need to create mysqli connection

    $mysqli = mysqli_connect("host", "user", "password", "database");
    $stmt = $mysqli->prepare("INSERT INTO comments(cName, cComment) VALUES (?, ?)");
    
    $name = 'Peter';
    $comment = 'Haloooo';
    $stmt->bind_param("ss", $name, $comment);
    $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?